﻿using RMS.BLL;
using RMS.Common;
using RMS.EF.Model;
using RMS.Manage.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMS.Manage.Web.Areas.RMS.Controllers
{
    [RMSAuthorize]
    public class UserController : BaseController
    {
        UserBll bll = new UserBll();
        RoleBll rbll = new RoleBll();
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="searchModel">筛选条件</param>
        /// <returns></returns>
        public ActionResult ListGetData(S_RMS_User searchModel)
        {
            //PageParams page = SetPageParams();
            PageParamsT<S_RMS_User> paget = SetPageParams(searchModel);
            var result_p = bll.GetByPage(paget);
            if (!result_p.IsSuccess)
            {
                return Json(result_p);
            }
            var result = SetPageJson(paget, result_p.Data);
            return Json(result);
        }
        /// <summary>
        /// 删除和批量删除
        /// </summary>
        /// <param name="id">Id字符串","分隔</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            var result = bll.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="id">数据Id</param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            RMS_User model = new RMS_User();
            if (!string.IsNullOrWhiteSpace(id))
            {
                var result = bll.GetById(id);
                if (!result.IsSuccess)
                {
                    ViewBag.ErrorMsg = result.Message;
                }
                else
                {
                    model = result.Data;
                }
            }
            return PartialView(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RMS_User model)
        {
            var result = new CommonOutPutVm();
            model.LastUpdateUser = UserId;
            #region 赋值并保存
            if (string.IsNullOrWhiteSpace(model.Id))
            {
                result = bll.Add(model);
            }
            else
            {
                result = bll.Updata(model);
            }
            #endregion
            return Json(result);
        }
        /// <summary>
        /// 关联角色
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        public ActionResult Relation_Role(string id)
        {
            ViewBag.Id = id;
            #region 获取已关联角色
            var result_ur = bll.Get_UserRole_ByUserId(id);
            if (!result_ur.IsSuccess)
            {
                ViewBag.ErrorMsg = result_ur.Message;
                ViewBag.RolesId = new List<string>();
            }
            else
            {
                var roles = result_ur.Data as List<RMS_User_Role>;
                ViewBag.RolesId = (from m in roles select m.RoleId).ToList(); 
            }
            #endregion
            #region 获取所有角色
            var allRoles = new List<RMS_Role>();
            var result_r = rbll.GetAllEnable();
            if (!result_r.IsSuccess)
            {
                ViewBag.ErrorMsg += result_r.Message;
            }
            else
            {
                allRoles = result_r.Data; 
            }
            #endregion
            return View(allRoles);
        }
        /// <summary>
        /// 关联角色
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Relation_Role(string id, string roleId)
        {
            var result = new CommonOutPutVm();
            result = bll.Relation_Role(id, roleId, UserId);
            return Json(result);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        public ActionResult ResetPwd(string id)
        {
            var result = new CommonOutPutVm();
            result = bll.ResetPwd(id);
            return Json(result);
        }
    }
}